var wsUri;
var ws;

window.addEventListener("load", function(evt) {


    /*
  wsUri  = "ws://" + window.location.host + "/coin"

  var newSocket = function() {
    ws           = new WebSocket(wsUri);
    ws.onopen = function(evt) {
        console.log("on open");
    }
    ws.onclose = function(evt) {
        console.log("on close");
      ws = null;
    }
    ws.onmessage = function(evt) {
        console.log("on message");
    }
    ws.onerror = function(evt) {
        console.log("on error");
    }
  };
  newSocket()
  */

    // wsUri  = "ws://" + window.location.host + "/ws/pair/detail";
    wsUri  = "ws://47.75.157.132:8082/ws/coin/detail";
    ws           = new WebSocket(wsUri);

    var first = true;

    setTimeout(function () {
        sendPing()
    }, 1000);


    function sendPing() {
        var msg = {ping: 1234567};
        req = JSON.stringify(msg);
        var data = {
            message_id: 10000,
            message: req
        };
        ws.send(JSON.stringify(data));
    }

    function sendMsg() {
        if (!first) {
            return
        }
        var req1 = JSON.stringify({coin: "btc", interval: "1min", length:200});
        var data1 = {
            message_id: 10102,
            message: req1
        };
        ws.send(JSON.stringify(data1));
        first = false;
    }

    setInterval(function () {
        sendPing()
    }, 5000);

    /*
    setTimeout(function () {

        var req1 = JSON.stringify({coin:"eth", slug:"bit-z"});
        var data1 = {
            message_id: 10103,
            message: req1
        };
        ws.send(JSON.stringify(data1));

    }, 10000);
    */

    ws.onmessage = function (ev) {
        sendMsg()
    }


});
